<script setup lang="ts">
import { ref } from 'vue'
import { OrderType } from '@/enums'
import type { UploaderAfterRead } from 'vant/lib/uploader/types'
import { upload } from '@/api/consult'
const emits = defineEmits<{
  (e: 'sendMessage', txt: string): void
  (e: 'sendImage', img: { id: string; url: string }): void
}>()
const props = defineProps<{
  status: OrderType
}>()

const msg = ref('')
const sendInfo = () => {
  if (msg.value) {
    emits('sendMessage', msg.value)
    msg.value = ''
  }
}
/*
发送图片
1：完成图片上传
2：将图片通过父组件发送出去
3：实现图片渲染
*/
const afterRead: UploaderAfterRead = async res => {
  console.log('上传的文件对象', res)
  if (Array.isArray(res)) {
    return
  }
  const res2 = await upload(res.file!)
  emits('sendImage', res2.data)
  console.log(res2.data)
}
</script>

<template>
  <div class="room-action">
    <van-field
      type="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      v-model.trim="msg"
      @keyup.enter="sendInfo"
      :disabled="status !== OrderType.ConsultChat"
    ></van-field>
    <van-uploader
      :after-read="afterRead"
      :preview-image="false"
      :disabled="status !== OrderType.ConsultChat"
    >
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
